package dal;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import bl.DBConnection;
import bl.List;
import bl.Parser;
import bl.Product;
import bl.User;



public class Thread implements Runnable{

	private Socket socket;
	private User user = new User();
	private Parser parser = new Parser();
	private Product product = new Product();
	private DBConnection dbconn = new DBConnection();
	private List onstock = new List();
	private String loginresult;
	private int regresult, addresult, outofstock = 0;
	

	public Thread (Socket socket) {
		
		this.socket = socket;
	}

	
	public void run(){
			
				try {
					String input = leseNachricht(socket);
					System.out.println("Input: " + input);
					parser.xmlread(input, user, product, onstock);
					
					//########################################################################################################
					//checkUpdates
					if(user.getRequest().equalsIgnoreCase("checkUpdates")){
						checkUpdates();
					
					//########################################################################################################
					//Registration
					}else if(user.getRequest().equalsIgnoreCase("regUser")){
						Registration();

					//########################################################################################################
					//Login
					}else if(user.getRequest().equalsIgnoreCase("loginUser")){
						Login();

					//########################################################################################################
					//Barcode
					}else if(user.getRequest().equalsIgnoreCase("sendBarcode")){
						Barcode();
					
					//########################################################################################################
					//Add Product
					}else if(user.getRequest().equalsIgnoreCase("addProduct")){
						addProduct();

					//########################################################################################################
					//sendList
					}else if(user.getRequest().equalsIgnoreCase("sendList")){
						sendList();
					
					//########################################################################################################
					//outOfStock
					}else if(user.getRequest().equalsIgnoreCase("outOfStock")){
						outOfStock();
						
					//########################################################################################################
					//getList
					}else if(user.getRequest().equalsIgnoreCase("getList")){
						getList();
					}
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					System.out.println("Thread SHICE");
					e.printStackTrace();
				}
				
	}
	
	public void checkUpdates() throws IOException{
		if(Integer.parseInt(user.getLatestversion()) != Integer.parseInt(user.getVersion())){
			schreibeNachricht(socket, parser.checkUpdates(false));
		}else{
			schreibeNachricht(socket, parser.checkUpdates(true));
			
		}
	}
	
	public void Registration() throws IOException{
		regresult = dbconn.registerUser(user.getUsername(), user.getPassword());
		switch (regresult) {
		case 1:
				schreibeNachricht(socket, parser.sendAcknowledge("Registration successfull"));
				System.out.println("--> Registration successfull");

			break;
		case 0:
				schreibeNachricht(socket, parser.sendAcknowledge("Registration failed"));
				System.out.println("--> Registration Error");
			break;
		case -2:
				schreibeNachricht(socket, parser.sendAcknowledge("Registration failed"));
				System.out.println("--> Registration Error");
			break;

		default:
			break;
		}
	}
	
	public void Login() throws IOException{
		loginresult = dbconn.loginUser(user.getUsername());
		if(loginresult != null){
			schreibeNachricht(socket, parser.sendAcknowledge(loginresult));
		}else{
			schreibeNachricht(socket, parser.sendAcknowledge("null"));
			
		}
	}
	
	public void Barcode() throws IOException{
		String productdescriptionxml = dbconn.getProduct(product.getBarcode());
		System.out.println(productdescriptionxml);
		schreibeNachricht(socket, productdescriptionxml);
	}
	
	public void addProduct() throws IOException{
		addresult = dbconn.addProduct(product);
		switch (addresult) {
		case 1:
				schreibeNachricht(socket, parser.sendAcknowledge("Adding successfull"));
				System.out.println("--> Adding successfull");

			break;
		case 0:
				schreibeNachricht(socket, parser.sendAcknowledge("Adding failed"));
				System.out.println("--> Adding Error");
			break;
		case -2:
				schreibeNachricht(socket, parser.sendAcknowledge("Adding failed"));
				System.out.println("--> Adding Error");
			break;

		default:
			break;
		}
	
	}

	public void sendList() throws IOException{
		
		addresult = dbconn.addToStock(product.getBuffer(), user.getUsername(), user.getPassword());
		switch (addresult) {
		case 1:
			schreibeNachricht(socket, parser.sendAcknowledge("Adding successfull"));
			System.out.println("--> Adding successfull");
			break;
		case 0:
			schreibeNachricht(socket, parser.sendAcknowledge("Adding failed"));
			System.out.println("--> Adding Error");
			product.resetBuffer();
			break;
		case -1:
			System.out.println(parser.getAlreadyOnStockList(DBConnection.onStockBuffer));
			schreibeNachricht(socket, parser.getAlreadyOnStockList(DBConnection.onStockBuffer));
			System.out.println("--> -1");
			DBConnection.onStockBuffer = new StringBuffer();
			product.resetBuffer();
			break;
		case -2:
			schreibeNachricht(socket, parser.sendAcknowledge("Product already on stock"));
			System.out.println("--> Product already on stock");
			product.resetBuffer();
			break;
			
		default:
			break;
		}
	}
	
	public void outOfStock() throws IOException{
		outofstock = dbconn.outOfStock(product.getBuffer(), user.getUsername(), user.getPassword());
		switch (outofstock) {
		case 1:
			schreibeNachricht(socket, parser.sendAcknowledge("List successfully updated"));
			System.out.println("--> List successfully updated");
			break;
		case 0:
			schreibeNachricht(socket, parser.sendAcknowledge("Updating Error"));
			System.out.println("--> Updating Error");
			product.resetBuffer();
			break;
		case -2:
			schreibeNachricht(socket, parser.sendAcknowledge("Product already on stock"));
			System.out.println("--> Product already on stock");
			product.resetBuffer();
			break;
			
		default:
			break;
		}
	
	}
	
	public void getList() throws IOException{
		
		String getresult = dbconn.getList(user);
		System.out.println(getresult);
		if(!getresult.equalsIgnoreCase(null)){
			
			schreibeNachricht(socket, getresult);
			System.out.println("--> Loading successfull");
		}else{
			
			schreibeNachricht(socket, parser.sendAcknowledge("Loading failed"));
			System.out.println("--> Loading Error");
		}
	}
	
	public void schreibeNachricht(Socket socket, String cmd) throws IOException { // You run this from the main
		
	    PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
	    pw.println(cmd);
	    pw.flush();
	}
	
	public String leseNachricht(Socket socket) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		String got = br.readLine();
		return got;
	}
    
}
